package com.hongyuan.sellfitness.ui.home.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomFragment;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.databinding.FragmentHomeBinding;
import com.hongyuan.sellfitness.ui.home.beans.SalerBean;
import com.hongyuan.sellfitness.ui.home.beans.UseQrBean;
import com.hongyuan.sellfitness.ui.home.view_mode.FragmentHomeViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;
import com.hongyuan.sellfitness.utils.HourMeterUtil;
import java.util.List;

public class HomeFragment extends CustomFragment<FragmentHomeBinding, FragmentHomeViewModel> implements HourMeterUtil.TimeCallBack{

    //二维码弹框
    private Dialog dialog;
    private UseQrBean qrBean;
    //计时对象
    private HourMeterUtil hourMeterUtil;

    //销售顾问
    private List<SalerBean.ListBean> mList;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FragmentHomeViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FragmentHomeViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        //初始化计时状态
        hourMeterUtil = new HourMeterUtil();
        hourMeterUtil.setTimeCallBack(this);
    }

    @Override
    public void initData() {

        //获取支持门店列表
        viewModel.clearParams();
        viewModel.requestData(Constants.GET_MEMEBER_OS);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.setSalers.observe(this, s -> {
            mList = s;
        });

        viewModel.uc.showCards.observe(this, s -> {
            CustomDialog.showMyCards(getContext(), viewModel.cardList, new CustomDialog.DialogClickMessage() {
                @Override
                public void dialogClick(View v, String message) {
                    int cardPosition = Integer.parseInt(message);

                    viewModel.useQrBean = new UseQrBean();
                    viewModel.useQrBean.setOs_id(String.valueOf(viewModel.cardList.get(cardPosition).getOs_id()));
                    viewModel.useQrBean.setCard_type_name(viewModel.cardList.get(cardPosition).getCard_type_name());
                    viewModel.my_card_id = String.valueOf(viewModel.cardList.get(cardPosition).getMy_card_id());
                    //检查进店还是离店
                    viewModel.clearParams().setParams("os_id",String.valueOf(viewModel.entity.get().getOs_id()));
                    viewModel.requestData(Constants.CHECK_COME_OR_OFF_STORE);
                }
            });
        });
        viewModel.uc.showSaler.observe(this, s -> {
            if(mList != null && mList.size() > 0){
                CustomDialog.scroller(getContext(), viewModel.rUtils.getUnitList(mList), "选择销售顾问", (v1, message,id) -> {
                    String telNum = viewModel.getSalerTel(mList,message);
                    if(BaseUtil.isValue(telNum)){
                        callTel(telNum);
                    }else{
                        ToastUtils.showShort("无效电话！");
                    }

                });
            }else{
                ToastUtils.showShort("暂无顾问！");
            }
        });

        viewModel.uc.showQrDialog.observe(this, useQrBean -> {
            qrBean = useQrBean;
            hourMeterUtil.startCount();
            dialog = CustomDialog.showQRScan(getContext(),useQrBean.getQrImgUrl(),useQrBean.getCard_type_name(),useQrBean.getIs_in());
        });
        viewModel.uc.closeQrDialog.observe(this, aBoolean -> {
            noShowLoading = false;
            dialog.dismiss();
            hourMeterUtil.onDestory();
        });
    }

    @Override
    public void onTime(int passedTime) {
        if(dialog != null && dialog.isShowing()){
            viewModel.clearParams().setParams("os_id",qrBean.getOs_id()).setParams("is_code",qrBean.getIs_code())
                    .setParams("is_type",qrBean.getIs_in().equals("0") ? "1" : "2");
            //不显示加载框
            noShowLoading = true;
            viewModel.requestData(Constants.CHECK_DO_CARD);
        }else{
            hourMeterUtil.onDestory();
        }
    }
}
